package com.zbbt.sportslottery.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.HuoDongQuanData;
import com.zbbt.sportslottery.bean.HuoDongQuanIsGet;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.util.KeyUtil;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.SharePreferUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.UmengShare;

public class RedBookActivity extends SimpleBaseActivity {
	private TextView d3_weixin, tv_right;
	private TextView d3_person;
	private UmengShare umengShare;
	private WebView webView;
	private LinearLayout layout;
	private LinearLayout share_layout;
	private LinearLayout.LayoutParams layoutParams;
	// 分享的红包地址
	private String shareUrl = "http://testwx.zgtcb.com/portal/pages/xinlecai/index.html";
	private int price;
	private Bitmap drawableToBitamp;
	private boolean isGetGain = false;// 是否领取体验券
	boolean isClick = false;
	private LinearLayout top;
	private String idString;
	private String url;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_redbook);
		umengShare = new UmengShare(this);
		Intent intent = getIntent();
		url = intent.getStringExtra("Url");
	}

	@Override
	protected void initView() {
		idString = SharePreferUtil.getString(Constant.USER_NAME, "");
		LogUtil.printD("dsdsdsdsds", idString);
		layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		Drawable drawable = context.getResources().getDrawable(R.drawable.fenxianghb);
		drawableToBitamp = drawableToBitamp(drawable);
		top = (LinearLayout) findViewById(R.id.ll_top);
		webView = (WebView) findViewById(R.id.red_data_webview);
		tv_right = (TextView) findViewById(R.id.tv_right);
		d3_person = (TextView) findViewById(R.id.d3_person1);
		d3_weixin = (TextView) findViewById(R.id.d3_weixin1);

		webView.getSettings().setJavaScriptEnabled(true); // 启用JavaScript
		webView.getSettings().setBuiltInZoomControls(true); // 控制页面缩放

		webView.setWebChromeClient(new WebChromeClient() {
			@Override
			public boolean onJsAlert(WebView webview, String url, final String message, final JsResult result) {
				top.setVisibility(View.VISIBLE);
				webview.loadUrl("javascript:btn_share()");
				result.cancel();
				return true;
			}
		});
		webView.loadUrl(url + "?mobile=" + idString);
	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	private Bitmap drawableToBitamp(Drawable drawable) {
		int w = drawable.getIntrinsicWidth();
		int h = drawable.getIntrinsicHeight();
		System.out.println("Drawable转Bitmap");
		Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
				: Bitmap.Config.RGB_565;
		Bitmap bitmap = Bitmap.createBitmap(w, h, config);
		// 注意，下面三行代码要用到，否在在View或者surfaceview里的canvas.drawBitmap会看不到图
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0, 0, w, h);
		drawable.draw(canvas);
		return bitmap;
	}

	@Override
	protected void setListener() {
		d3_person.setOnClickListener(this);
		d3_weixin.setOnClickListener(this);
		tv_right.setOnClickListener(this);
		top.setOnClickListener(this);
	}

	/**
	 * 
	 * @MethodName:convertStringToIcon
	 * @Description: String 转bitmap
	 * @author：GaomingShuo
	 * @date：2015-5-20 下午1:49:15
	 * @param st
	 * @return Bitmap
	 */
	public static Bitmap convertStringToIcon(String st) {
		// OutputStream out;
		Bitmap bitmap = null;
		try {
			// out = new FileOutputStream("/sdcard/aa.jpg");
			byte[] bitmapArray;
			bitmapArray = Base64.decode(st, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
			// bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
			return bitmap;
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	// 891802
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.d3_weixin1:
			top.setVisibility(View.GONE);
			isClick = true;
			WeiXinShareContent weixinContent = new WeiXinShareContent();
			weixinContent.setTitle("新乐彩大派奖");
			weixinContent.setShareImage(new UMImage(context, drawableToBitamp));
			weixinContent.setShareContent("您的好友正在参与新乐彩抢红包活动，点击参与活动试试手气吧!");
			weixinContent.setTargetUrl(shareUrl);
			UmengShare.mController.setShareMedia(weixinContent);
			UmengShare.mController.postShare(context, SHARE_MEDIA.WEIXIN, new SnsPostListener() {
				@Override
				public void onStart() {}

				@Override
				public void onComplete(SHARE_MEDIA arg0, int eCode, SocializeEntity arg2) {
					if (eCode == 200) {
//						ToastUtil.showShort("分享成功");
					} else {
						String eMsg = "";
						if (eCode == -101) {
							eMsg = "没有授权";
						}
						ToastUtil.showShort("分享失败");
					}
				}
			});
			break;
		case R.id.d3_person1:
			top.setVisibility(View.GONE);
			isClick = true;
			CircleShareContent circleMedia = new CircleShareContent();
			circleMedia.setShareImage(new UMImage(context, shareUrl));
			circleMedia.setTitle("新乐彩大派奖");
			circleMedia.setShareImage(new UMImage(context, drawableToBitamp));
			circleMedia.setShareContent("您的好友正在参与新乐彩抢红包活动，点击参与活动试试手气吧!");
			circleMedia.setTargetUrl(shareUrl);
			UmengShare.mController.setShareMedia(circleMedia);
			UmengShare.mController.postShare(context, SHARE_MEDIA.WEIXIN_CIRCLE, new SnsPostListener() {
				@Override
				public void onStart() {}

				@Override
				public void onComplete(SHARE_MEDIA arg0, int eCode, SocializeEntity arg2) {
					if (eCode == 200) {
//						ToastUtil.showShort("分享成功");
					} else {
						String eMsg = "";
						if (eCode == -101) {
							eMsg = "没有授权";
						}
						ToastUtil.showShort("分享失败");
					}
				}
			});
			break;
		case R.id.tv_right:
			goActivity2(MainActivity.class, KeyUtil.GOTO_FRAGMENT, KeyUtil.FRAG_USER);
			break;
		case R.id.ll_top:
			top.setVisibility(View.GONE);
			break;
		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = UmengShare.mController.getConfig().getSsoHandler(requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

	@Override
	protected void initData() {

	}

}
